package rq.optimize.luban.example;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

class ImageAdapter2 extends RecyclerView.Adapter<ImageAdapter2.ImageHolder> {
    private Context mContext;
    private List<ImageBean2> mImageList;

    ImageAdapter2() {
    }

    @Override
    public ImageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        return new ImageHolder(LayoutInflater.from(mContext).inflate(R.layout.item_image2, parent, false));
    }

    @Override
    public void onBindViewHolder(ImageHolder holder, int position) {
        ImageBean2 image = mImageList.get(position);
        holder.originArg.setText(image.getOriginArg());
        holder.thumbArg.setText(image.getThumbArg());
        Glide.with(mContext).load(image.getImage()).into(holder.image);
        Glide.with(mContext).load(image.getImage2()).into(holder.image2);
        Log.e("ImageAdapter2", position + ">>:" + image.getImage() + "\n:" + image.getImage2());
    }

    @Override
    public int getItemCount() {
        return mImageList == null ? 0 : mImageList.size();
    }

    public <T> void setData(List<T> photos, ArrayList<File> file) {
        List<ImageBean2> res = new ArrayList<>();
        for (int i = 0; i < photos.size(); i++) {
            int[] originSize = computeSize((File) photos.get(i));
            int[] thumbSize = computeSize(file.get(i));
            String originArg = String.format(Locale.CHINA, "原图参数：%d*%d, %dk", originSize[0], originSize[1], ((File) photos.get(i)).length() >> 10);
            String thumbArg = String.format(Locale.CHINA, "压缩后参数：%d*%d, %dk", thumbSize[0], thumbSize[1], file.get(i).length() >> 10);
            res.add(new ImageBean2(originArg, thumbArg, ((File) photos.get(i)).getAbsolutePath(), file.get(i).getAbsolutePath()));
        }
        this.mImageList = res;
        notifyDataSetChanged();
    }

    private int[] computeSize(File srcImg) {
        int[] size = new int[2];
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inSampleSize = 1;
        BitmapFactory.decodeFile(srcImg.getAbsolutePath(), options);
        size[0] = options.outWidth;
        size[1] = options.outHeight;
        return size;
    }

    class ImageHolder extends RecyclerView.ViewHolder {
        private TextView originArg;
        private TextView thumbArg;
        private ImageView image;
        private ImageView image2;

        ImageHolder(View view) {
            super(view);

            originArg = view.findViewById(R.id.origin_arg);
            thumbArg = view.findViewById(R.id.thumb_arg);
            image = view.findViewById(R.id.image);
            image2 = view.findViewById(R.id.image2);

        }
    }
}